<?php
namespace Wap\Controller;
use Common\Model\CommentModel;
use Common\Model\BusinessModel;
use Common\Model\ExchangeModel;
use Common\Model\NoticeModel;
use Common\Model\SmsModel;
use Common\Model\UserModel;
use Wap\BaseController;
use Common\Model\UtilsModel;
class ToyController extends BaseController {
    private $pageSize=20;

    function _initialize(){
        parent::_initialize();
        $uid=session('uid')+0;
        UserModel::addUserValByAction($uid,OPT_ENTER_TOY);

        $tip=S('tip_'.$uid);
        if($tip) {
            session('tip', S('tip_' . $uid));
            $this->smarty->assign('userinfo', session());
        }
    }

    public function index(){
        $uid=session('uid')+0;
        $red=NoticeModel::checkRedPrompt($uid,NoticeModel::NOTICE_TYPE_TOY);
        $this->smarty->assign('prompt',$red);


        $this->smarty->assign('title','易物--'.C('SITE_NAME'));
        $this->smarty->assign('innerTitle','易物');

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $itemTypeKv     = ExchangeModel::getItemType();
        $itemStatusKv   = array(
            ExchangeModel::ITEM_STATUS_ALL=>'全部',
            ExchangeModel::ITEM_STATUS_SUCCESS=>'成功',
        );

        $this->smarty->assign('itemTypeKv',$itemTypeKv);
        $this->smarty->assign('itemStatusKv',$itemStatusKv);


        
        $this->smarty->display("Toy/index.html");
    }

    //加载更多
    public function loadMore(){
        $page           = I('get.page',2,'intval');
        $type           = I('get.type',0,'intval');
        $status         = I('get.status',0,'intval');



        $data = ExchangeModel::getShowItemList($status,$type,$page,$this->pageSize);
        $this->smarty->assign('list',$data['data']);

        $this->smarty->assign('itemSuccess',ExchangeModel::ITEM_STATUS_SUCCESS);
        $html = $this->smarty->fetch('Toy/index_list.html');
        $this->ajaxReturn(array('status'=>0, 'dataHTML'=>$html, 'isPage'=>$data['nextPage'],'hasData'=>$data['total']));
    }

    //物品详情
    public function detail(){
        $uid=session('uid')+0;
        $itemId = I("get.itemId",0,'intval');

       // if(!$id) redirect(U('Toy/index'));//数据为空返回列表

        $info=ExchangeModel::getItemDetail($itemId);
        if($info['status']!=0){
            $this->error("指定宝贝并不存在",u('index'));
        }

        $data=$info['data'];
        if($data['ownerId']!=$uid && ($data['status']==ExchangeModel::ITEM_STATUS_AUDIT || $data['status']==ExchangeModel::ITEM_STATUS_AUDIT_REFUSE)){
            $this->error("宝贝尚未通过审核",u('index'));
        }

        if($this->isWechat) {
            $domain=UtilsModel::getFullDomain();
            //微信分享相关
            $this->smarty->assign('isWxShare', true);
            $this->smarty->assign('wxTitle', $data['desc']);
            $this->smarty->assign('wxDesc',  $data['desc']);
            $this->smarty->assign('wxLink', $domain.U('detail?itemId=' . $itemId . '&fromUid=' . $uid));
            $this->smarty->assign('wxImage', C('UPLOAD_RESOURCE_PREFIX').$data['headpic']);
            $this->smarty->assign('wxObjType',OBJ_TYPE_TOY);
            $this->smarty->assign('wxObjID',$itemId);
        }

        $canExchange=0;
        if($data['status']==ExchangeModel::ITEM_STATUS_ONLINE || $data['status']==ExchangeModel::ITEM_STATUS_IN_TRADING){
            if($data['ownerId']!=$uid){
                $canExchange=1;
            }
        }
        $this->smarty->assign('canExchange',$canExchange);

        $album=explode(',',$data['album']);
        $this->smarty->assign('album',$album);
        $this->smarty->assign('albumSize',count($album));

        $this->smarty->assign('obj',$data);

        $this->smarty->assign('title','宝贝详情--'.C('SITE_NAME'));
        $this->smarty->assign('innerTitle','宝贝详情');

        $this->smarty->assign('haveExchange',ExchangeModel::haveCanExchangeItem($uid));

        $this->smarty->display("Toy/detail.html");
    }

    public function apply(){
        $uid=session('uid')+0;
        $mainItemId=I('get.id',0,'intval');
        if($mainItemId==0){
            $this->error('指定的宝贝并不存在',u('index'));
        }

        $ret=ExchangeModel::getItemDetail($mainItemId);
        if($ret['status']!=0){
            $this->redirect('index');
        }
        $item=$ret['data'];
        switch ($item['status']){
            case ExchangeModel::ITEM_STATUS_ONLINE:
            case ExchangeModel::ITEM_STATUS_IN_TRADING:
                break;
            default:
                $this->error('选定宝贝所处状态不可交换',u('index'));
                break;
        }
        $list=ExchangeModel::getShowItemList(ExchangeModel::ITEM_STATUS_ONLINE,ExchangeModel::ITEM_TYPE_ALL,1,50,$uid);

        $this->smarty->assign("list",$list['data']);
        $this->smarty->assign("mainItemId",$mainItemId);
        $this->smarty->assign('title','申请交换--'.C('SITE_NAME'));
        $this->smarty->assign('title','申请交换');
        $this->smarty->display("Toy/apply.html");
    }
    public function applyDo(){
        $uid=session('uid')+0;
        $mainItemId     = I('post.main',0,'intval');
        $slaveItemId    = I('post.slave',0,'intval');
        if($mainItemId<=0 || $slaveItemId<=0){
            $this->ajaxReturn(array('status'=>1,'msg'=>'参数错误'));
        }
        $this->ajaxReturn(ExchangeModel::applyDo($mainItemId,$slaveItemId,$uid));
    }


    /**
     * 评价页面
     */
    public function feedback(){
        $id=I('get.id',0,'intval');
        if($id<1){
            $this->error('请指定要评价的交换记录');
        }

        $this->smarty->assign("id",$id);

        //$this->smarty->assign("backURL",u('Toy/deal'));
        $this->smarty->assign("title",'交换评价--'.C('SITE_NAME'));
        $this->smarty->assign("innerTitle",'交换评价');
        $this->smarty->display("Toy/feedback.html");
    }

    /**
     * 评价
     */
    public function comment(){
        $objType=OBJ_TYPE_TOY_RECORD;
        $objID=I('post.id',0,'int');
        $parentID=I('post.parentID',0,'int');//父级评论
        $content=I('post.content','','strip_tags');//
        $content=preg_replace('/@.*:/','',$content);
        $album=I('post.album');
        $albumstr=implode(',',$album);

        if(!session('uid')){
            $this->ajaxReturn(array('status'=>99, 'msg'=>'请先登录', 'uri'=>U('User/login')."&uri=".urlencode(U('Comment/comment')."&type=".$objType."&id=".$objID) ));
        }

        if(!$objType || !$objID){
            $this->ajaxReturn(array('status'=>1, 'msg'=>'参数错误'));
        }

        if(true !== $maskStr = checkCleanWord($content) ){
            $this->ajaxReturn(array('status'=>1, 'msg'=>'评论内容中不能包含'.$maskStr));
            return;
        }
        $uinfo = array(
            'uid'=>session('uid'),
            'nickname'=>session('nickname')
        );


        $data = CommentModel::commentNew($objType, $objID, $parentID, $content, $uinfo,0,$albumstr);
        $this->ajaxReturn($data);
    }


    public function myPrefer(){
        $uid=session('uid')+0;

        $this->smarty->assign('title','我的上架--'.C('SITE_NAME'));

        $list = ExchangeModel::getMine($uid,1,88);

        $this->smarty->assign('list',$list['data']);

        $statusKV=ExchangeModel::getItemStatus();
        $this->smarty->assign('statusKV',$statusKV);

        $this->smarty->assign('statusAudit',ExchangeModel::ITEM_STATUS_AUDIT);
        $this->smarty->assign('statusOnline',ExchangeModel::ITEM_STATUS_ONLINE);
        $this->smarty->assign('statusInTrading',ExchangeModel::ITEM_STATUS_IN_TRADING);
        $this->smarty->assign('statusOffline',ExchangeModel::ITEM_STATUS_OFFLINE);
        $this->smarty->assign('statusSuccess',ExchangeModel::ITEM_STATUS_SUCCESS);
        $this->smarty->assign('statusRefuse',ExchangeModel::ITEM_STATUS_AUDIT_REFUSE);
        $this->smarty->assign('effectiveTime',ExchangeModel::ITEM_EFFECTIVE_SECOND);


        $this->smarty->assign('title','我的上架--'.C('SITE_NAME'));
        $this->smarty->assign('innerTitle','我的上架');
        $this->smarty->assign('backURL',u('index'));

        $this->smarty->display("Toy/myPrefer.html");
    }


    public function deal(){
        $uid=session('uid')+0;
        $this->smarty->assign('mainUid',$uid);

        NoticeModel::resetRedPrompt($uid,NoticeModel::NOTICE_TYPE_TOY);

        $list=ExchangeModel::getTradeList(ExchangeModel::RECORD_STATUS_ALL,1,30,$uid);

        $this->smarty->assign('list',$list['data']);
        $this->smarty->assign('userPhone',$list['phone']);
        $this->smarty->assign('statusWait',ExchangeModel::RECORD_STATUS_WAIT);
        $this->smarty->assign('statusAgree',ExchangeModel::RECORD_STATUS_AGREE);
        $this->smarty->assign('statusRefuse',ExchangeModel::RECORD_STATUS_REFUSE);

        $this->smarty->assign('backURL',u('index'));
        $this->smarty->assign('title','交易信息--'.C('SITE_NAME'));
        $this->smarty->assign('innerTitle','交易信息');
        $this->smarty->display("Toy/deal.html");
    }

    public function publish(){
        $uid        =session('uid')+0;
        $nickname   =session('nickname');
        if(IS_POST){//提交
            $desc       = I('post.desc');
            $intention  = I('post.intention');
            $album      = I('post.album');
            $itemId     = I('post.id',0,'intval');
            $headpic    = I('post.headpic');
            $position    = I('post.position');
            $value    = I('post.value');
            $this->ajaxReturn(ExchangeModel::editItem($itemId,$uid,$nickname,$headpic,$album,$desc,$intention,$position,$value));
        }
        else{//查看
            $itemId=I('get.id',0,'intval');
            if($itemId>0){
                $data=ExchangeModel::getItemDetail($itemId);
                if($data['status']!=0){
                    $this->error($data['msg']);
                }

                $this->smarty->assign('obj',$data['data']);
                $album=explode(',',$data['data']['album']);
                $this->smarty->assign('album',$album);
            }else{//发新，判断手机号是否绑定
                if(!UserModel::getBindPhone($uid)){
                    $this->error("请选绑定手机号",u('User/bind?url='.urlencode(u('Toy/publish'))));
                }
            }
            $uc=UserModel::getUserChangeFul($uid);
            if($uc['data']['point'] < TOY_PUBLISH_NEED_POINT){
                $pointOK=0;
            }else{
                $pointOK=1;
            }

            $this->smarty->assign('pointOK',$pointOK);

            $leftTimes=ExchangeModel::getLeftPublishTimes($uid);
            $this->smarty->assign('leftTimes',$leftTimes);
            $this->smarty->assign('title','宝贝上架--'.C('SITE_NAME'));
            $this->smarty->assign('innerTitle','宝贝上架');
            $this->smarty->display('Toy/publish.html');
        }


    }


    public function showReason(){
        $itemId=I('post.id',0,'intval');
        if(!$itemId){
            $this->ajaxReturn(array('status'=>1,'msg'=>'请指定宝贝'));
        }
        $this->ajaxReturn(ExchangeModel::showReason($itemId));
    }


    public function matchSuccess(){
        $itemId=I('get.itemId',0,'intval');
        $tradeId=I('get.tradeId',0,'intval');
        if($itemId>$tradeId){
            $item=ExchangeModel::getItemDetail($itemId);
            if($item['status']!=0){
                $this->error('指定的宝贝并不存在',u('index'));
            }

            $item=$item['data'];

            $tradeId=$item['tradeId'];
            $tradeDetail = ExchangeModel::getTradeDetail($tradeId);
            if($tradeDetail['slaveItem']==$itemId){
                $slaveItem  = $item;
                $tmp        = ExchangeModel::getItemDetail($tradeDetail['mainItem']);
                $mainItem   = $tmp['data'];
            }else{
                $tmp        = ExchangeModel::getItemDetail($tradeDetail['slaveItem']);
                $slaveItem  = $tmp['data'];
                $mainItem   = $item;
            }
        }elseif($itemId<$tradeId){
            $tradeDetail = ExchangeModel::getTradeDetail($tradeId);
            $mainItem        = ExchangeModel::getItemDetail($tradeDetail['mainItem']);
            $mainItem        = $mainItem['data'];
            $slaveItem        = ExchangeModel::getItemDetail($tradeDetail['slaveItem']);
            $slaveItem        = $slaveItem['data'];
        }else{
            $this->error('请指定要查看的信息',u('index'));
        }


        $this->smarty->assign('tradeItem',$tradeDetail);
        $this->smarty->assign('mainItem',$mainItem);
        $this->smarty->assign('slaveItem',$slaveItem);


        $comment=CommentModel::getCommentTop(OBJ_TYPE_TOY_RECORD,$tradeId);

        $this->smarty->assign('commentList',$comment);

        $this->smarty->assign('title','交换信息--'.C('SITE_NAME'));
        $this->smarty->assign('innerTitle','交换信息');
        $this->smarty->display("Toy/matchSuccess.html");
    }


    /**
     * 拒绝交换
     */
    public function refuse(){
        $uid=session('uid')+0;
        $recordId=I('post.recordId',0,'intval');
        $result=ExchangeModel::refuse($recordId,$uid);
        $this->ajaxReturn($result);
    }


    /**
     * 同意交换
     */
    public function agree(){
        $uid=session('uid')+0;
        $recordId=I('post.recordId',0,'intval');
        $result=ExchangeModel::agree($recordId,$uid);
        $this->ajaxReturn($result);
    }


    /**
     * 宝贝上架下架
     */
    public function onlineOffline(){
        $itemId=I('post.id',0,'intval');
        $uid=session('uid')+0;
        $result=ExchangeModel::onlineOffline($itemId,$uid);
        $this->ajaxReturn($result);
    }



    /**
     * 删除宝贝
     */
    public function deleteItem(){
        $itemId=I('post.id',0,'intval');
        $uid=session('uid')+0;
        $result=ExchangeModel::deleteItem($itemId,$uid);
        $this->ajaxReturn($result);
    }

    /**
     * 删除交换记录
     */
    public function deleteTrade(){
        $tradeId=I('post.id',0,'intval');
        $uid=session('uid')+0;
        $result=ExchangeModel::deleteTrade($tradeId,$uid);
        $this->ajaxReturn($result);
    }
}